var _diff = _interopRequireDefault(require("../../public/utils/diff"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

Component({
    properties: {
        canvasId: String
    },
    data: {
        src: "",
        width: 0,
        height: 0
    },
    ready: function() {
        var t = getCurrentPages(), e = t[t.length - 1];
        (this.curPage = e).$$radarAi = e.$$radarAi || Promise.resolve(), this.ctx = wx.createCanvasContext(this.data.canvasId, this);
    },
    detached: function() {
        this.curPage.$$radarAi = void 0;
    },
    methods: {
        init: function(t, e) {
            var i = this;
            if ((0, _diff.default)(t, this.radarData || []).length) {
                var a = ".img";
                this.radarData = t, wx.lazyFetch({
                    selector: a,
                    scope: this,
                    handler: function() {
                        return i.draw(a, e);
                    }
                });
            }
        },
        draw: function(t, a) {
            var n = this;
            wx.createSelectorQuery().in(this).select(t).boundingClientRect(function(t) {
                var e = t.width, i = t.height;
                n.setData({
                    width: e,
                    height: i
                }, function() {
                    return n.initOptions(e, i, a);
                });
            }).exec();
        },
        initOptions: function(i, a, n) {
            var r = this;
            this.curPage.$$radarAi = this.curPage.$$radarAi.then(function() {
                return new Promise(function(e, t) {
                    try {
                        r.ctx.clearRect(0, 0, i, a), r.mCount = r.radarData.length, r.xCenter = i / 2, r.yCenter = a / 2, 
                        r.mRadius = (a < i ? r.yCenter : r.xCenter) - n, r.mAngle = 2 * Math.PI / r.mCount, 
                        r.drawPolygon(r.ctx), r.drawLines(r.ctx), r.drawText(r.ctx), r.drawRegion(r.ctx), 
                        r.ctx.draw(!0, function() {
                            return r.canvasToTempFilePath(e);
                        });
                    } catch (t) {
                        console.log(t), r.radarData = [], e();
                    }
                });
            });
        },
        canvasToTempFilePath: function(e) {
            var i = this;
            wx.sleep(200).then(function() {
                return wx.fPromise("canvasToTempFilePath", {
                    canvasId: i.data.canvasId,
                    quality: 1
                }, i);
            }).then(function(t) {
                i.setData({
                    src: t.tempFilePath
                }), e();
            }).catch(function() {
                i.radarData = [], e();
            });
        },
        drawPolygon: function(t) {
            t.save(), t.setStrokeStyle("#88d987");
            for (var e = this.mRadius / 2, i = 2; 0 < i; i--) {
                t.beginPath();
                for (var a = e * i, n = 0; n < this.mCount; n++) {
                    var r = this.xCenter + a * -Math.sin(this.mAngle * n), s = this.yCenter + a * -Math.cos(this.mAngle * n);
                    t.lineTo(r, s);
                }
                t.setFillStyle("#daf5da"), t.fill(), t.closePath(), t.stroke();
            }
            t.restore();
        },
        drawLines: function(t) {
            t.save(), t.setStrokeStyle("#a6e19c"), t.beginPath();
            for (var e = 0; e < this.mCount; e++) {
                var i = this.xCenter + this.mRadius * -Math.sin(this.mAngle * e), a = this.yCenter + this.mRadius * -Math.cos(this.mAngle * e);
                t.moveTo(this.xCenter, this.yCenter), t.lineTo(i, a);
            }
            t.closePath(), t.stroke(), t.restore();
        },
        drawText: function(t) {
            var e = 12, i = this.radarData;
            t.fillStyle = "#09bb07", t.font = "12px Microsoft Yahei", t.save();
            for (var a = 0; a < this.mCount; a++) {
                var n = this.xCenter + this.mRadius * -Math.sin(this.mAngle * a), r = this.yCenter + this.mRadius * -Math.cos(this.mAngle * a) + 6;
                0 <= this.mAngle * a && this.mAngle * a <= Math.PI / 2 ? t.fillText(i[a][0], n - 60 - e, r) : this.mAngle * a > Math.PI / 2 && this.mAngle * a <= Math.PI ? t.fillText(i[a][0], n - 60 - e, r) : (this.mAngle * a > Math.PI && (this.mAngle, 
                Math.PI), t.fillText(i[a][0], n + e, r));
            }
            t.restore();
        },
        drawRegion: function(t) {
            var e = this.radarData;
            t.save();
            for (var i = 0; i < this.mCount; i++) {
                t.beginPath();
                var a = e[i][2], n = e[i][1], r = i + 1;
                r === this.mCount && (r = 0);
                var s = e[r][1];
                t.setStrokeStyle(a), t.moveTo(this.xCenter, this.yCenter);
                var h = this.xCenter + this.mRadius * -Math.sin(this.mAngle * i) * n, o = this.yCenter + this.mRadius * -Math.cos(this.mAngle * i) * n, l = this.xCenter + this.mRadius * -Math.sin(this.mAngle * r) * s, c = this.yCenter + this.mRadius * -Math.cos(this.mAngle * r) * s;
                t.lineTo(h, o), t.lineTo(l, c), t.setFillStyle(a), t.fill(), t.closePath(), t.stroke();
            }
            t.restore();
        }
    }
});